import { Component, EventEmitter, Injector, Input, Output } from '@angular/core';
import { AppConsts } from '@shared/AppConsts';
import { AppComponentBase } from '@shared/common/app-component-base';
import { map as _map, filter as _filter } from 'lodash-es';
import { FileUploadHelper } from '@shared/helpers/FileUploadHelper';
import { ContentListConfigItem, IdAndOrderOfGuid, MenuInfoConfig, MenuInfoServiceProxy, SetStatusWithMenuIdInput, SwapSortNumberWithMenuIdInput } from '@shared/service-proxies/service-proxies';


@Component({
    selector: 'image-text-list',
    templateUrl: './image-text-list.component.html',
})
export class ImageTextListComponent extends AppComponentBase {

    @Input() listConfig: ContentListConfigItem[]= [];

    @Input() records:  { [key: string]: any }[] = [];
    @Input() kind: number;
    
    @Input()  permissions :  { [key: string]: boolean } = {};

    @Input()  dataKey: string;



    @Output() editModule: EventEmitter<any> = new EventEmitter<any>();
   
    constructor(
        injector: Injector,
        private _menuInfoServiceProxy: MenuInfoServiceProxy
    ) {
        super(injector);
    }

    upsort(record: { [key: string]: any }) {
        let index = this.records.findIndex(t=>t.id === record.id);
        if(index === 0){
            this.notify.warn(this.l('AlreadyBeTheFirst'));
            return;
        }
        let preIndex = index - 1;
        let preItem = this.records[preIndex];

        let order = record.order;
        record.order = preItem.order;
        preItem.order = order;
        
        this._menuInfoServiceProxy.swapImageTextSortNumber(new SwapSortNumberWithMenuIdInput({
            idOrderList:[
                new IdAndOrderOfGuid({id:record.id, order: record.order}),
                new IdAndOrderOfGuid({id:preItem.id, order: preItem.order})
            ],
            menuId: this.kind,
            relateId : null
        })).subscribe(result=>{
            this.notify.success(this.l('OrderSuccessSuccessfully'));
            [this.records[index],this.records[preIndex]] = [this.records[preIndex],this.records[index]];
        });
    }

    downSort(record: { [key: string]: any }) {
        let index = this.records.findIndex(t=>t.id === record.id);
        if(index === this.records.length - 1){
            this.notify.warn(this.l('AlreadyBeTheLast'));
            return;
        }

        let nextIndex = index + 1;
        let nextItem = this.records[nextIndex];

        let order = record.order;
        record.order = nextItem.order;
        nextItem.order = order;

        this._menuInfoServiceProxy.swapImageTextSortNumber(new SwapSortNumberWithMenuIdInput({
            idOrderList:[
                new IdAndOrderOfGuid({id:record.id, order: record.order}),
                new IdAndOrderOfGuid({id:nextItem.id, order: nextItem.order})
            ],
            menuId: this.kind,
            relateId : null
        })).subscribe(result=>{
            this.notify.success(this.l('OrderSuccessSuccessfully'));
            [this.records[index],this.records[nextIndex]] = [this.records[nextIndex],this.records[index]];
        })
    }

    releaseState(record: { [key: string]: any }) {
        let releaseList: string[] = [];
        if (record) {
            releaseList = [record.id]
        }
        if (releaseList.length == 0) return;

        this._menuInfoServiceProxy.setImageTextStatus(new SetStatusWithMenuIdInput({
            idList: releaseList,
            isActive: true,
            menuId: this.kind
        })).subscribe(result => {
            this.notify.success(this.l('ReleaseSuccessfully'));
            record.isActive = true;
        });
    }

    cancelReleaseState(record: { [key: string]: any }) {
        let releaseList: string[] = [];
        if (record) {
            releaseList = [record.id]
        }
        if (releaseList.length == 0) return;

        this._menuInfoServiceProxy.setImageTextStatus(new SetStatusWithMenuIdInput({
            idList: releaseList,
            isActive: false,
            menuId: this.kind
        })).subscribe(result => {
            this.notify.success(this.l('CancelReleaseSuccessfully'));
            record.isActive = false;
        });
    }

    deleteSingle(record: { [key: string]: any }) {
        this.message.confirm(this.l('DeleteWarningMessage'), this.l('AreYouSure'),
            (isConfirmed) => {
                if (!isConfirmed) return;
                this._menuInfoServiceProxy.deleteImageText(record.id, this.kind).subscribe(result => {
                    this.notify.success(this.l('SuccessfullyDeleted'));
                    this.records = this.records.filter(t => t.id !== record.id);
                })
            })
    }

    lookOver(picurl: string) {
        abp.event.trigger(AppConsts.EventName.showImageView, FileUploadHelper.getUrl(picurl));
    }

    getSmallUrl(picurl: string) {
        return FileUploadHelper.getSmallUrl(picurl, 100, 80);
    }

    editModuleFunc(record: { [key: string]: any }) {
        this.editModule.emit({record:record, key:this.dataKey});
    }

    exDiyFunc(record, funcName:string){
        this[funcName](record);
    }
    getDiyFuncResult(record, funcName:string){
        return this[funcName](record);
    }
}
